package com.lagou.rpc.consumer.config;



import com.alibaba.fastjson.JSON;
import com.lagou.ZookeeperCreateNode;
import com.lagou.ZookeeperSelectNode;
import com.lagou.ZookeeperUpdateNode;
import com.lagou.rpc.consumer.util.SpringUtil;
import org.apache.zookeeper.KeeperException;
import org.apache.zookeeper.WatchedEvent;
import org.apache.zookeeper.Watcher;
import org.apache.zookeeper.ZooKeeper;
import org.apache.zookeeper.data.Stat;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.concurrent.CountDownLatch;

/**
 * @author: jie.zhao
 * @create: 2021/05/31 15:14
 */
@Component
public class ZookeeperSessionCreateSelectNode implements Watcher {

    private static CountDownLatch countDownLatch = new CountDownLatch(1);

//    private static ZooKeeper zooKeeper;

//    @Resource(name = "zookeeper")
//    ZooKeeper zooKeeper;

    public void init(){
        try {
            ZooKeeper zooKeeper =(ZooKeeper) SpringUtil.getBean("zookeeper");
            try {
                ZookeeperSelectNode zookeeperSelectNode = new ZookeeperSelectNode();
                ZookeeperCreateNode zookeeperCreateNode = new ZookeeperCreateNode();
                ZookeeperUpdateNode zookeeperUpdateNode = new ZookeeperUpdateNode();
                HashMap<String, String> config = zookeeperSelectNode.getServiceNodeAndData(zooKeeper);
                Stat exists = zooKeeper.exists("/serviceData", true);
                if(exists!=null) {
                    zookeeperUpdateNode.updateNode(zooKeeper, JSON.toJSONString(config), "/serviceData");
                }else {
                    zookeeperCreateNode.createNode2(zooKeeper, JSON.toJSONString(config), "/serviceData");
                }
            }
            catch (Exception e) {
                e.printStackTrace();
            }
            countDownLatch.await();

        }  catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void process(WatchedEvent watchedEvent) {
        ZookeeperSelectNode zookeeperSelectNode = new ZookeeperSelectNode();
        ZookeeperCreateNode zookeeperCreateNode = new ZookeeperCreateNode();
        ZookeeperUpdateNode zookeeperUpdateNode = new ZookeeperUpdateNode();
        if(watchedEvent.getState()==(Event.KeeperState.SyncConnected)) {
            System.out.println("链接回调");

//            try {
//
//                HashMap<String, String> config = zookeeperSelectNode.getServiceNodeAndData(zooKeeper);
//                Stat exists = zooKeeper.exists("/serviceData", true);
//                if(exists!=null) {
//                    zookeeperUpdateNode.updateNode(zooKeeper, config.toString(), "/serviceData");
//                }else {
//                    zookeeperCreateNode.createNode2(zooKeeper, config.toString(), "/serviceData");
//                }
//            }
//            catch (Exception e) {
//                e.printStackTrace();
//            }
        }
        if(watchedEvent.getType()==(Event.EventType.NodeChildrenChanged)){
            try {
                ZooKeeper zooKeeper =(ZooKeeper) SpringUtil.getBean("zookeeper");
                HashMap<String, String> config = zookeeperSelectNode.getServiceNodeAndData(zooKeeper);
                Stat exists = zooKeeper.exists("/serviceData", true);
                if(exists!=null) {
                    zookeeperUpdateNode.updateNode(zooKeeper, JSON.toJSONString(config), "/serviceData");
                }else {
                    zookeeperCreateNode.createNode2(zooKeeper, JSON.toJSONString(config), "/serviceData");
                }
            } catch (KeeperException e) {
                e.printStackTrace();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}
